package com.aaa.book_mall.dao.impl;

import com.aaa.book_mall.dao.TypeDao;
import com.aaa.book_mall.util.JDBCUtil;
import org.springframework.stereotype.Repository;
import com.aaa.book_mall.entity.Type;
import java.util.List;
import java.util.Map;

/**
 * @Description: 类型表实现类
 * @ClassName: TypeDaoImpl
 * @Author: fengyun\付亚星
 * @Date: 2021/1/19 21:26
 * @Version: 1.0
 */
@Repository
public class TypeDaoImpl implements TypeDao {
    @Override
    public List<List> getAllTypeOut() {
        String sql="select * from t_type";
        List<List> lists = JDBCUtil.queryList(sql);
        return lists;
    }

    @Override
    public List<Map<String, Object>> getAllType() {
        String sql = "select * from t_type";
        List<Map<String, Object>> maps = JDBCUtil.queryListMap(sql);
        if(maps.size()>0){
            return maps;
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> getNumberType(Type type) {
        StringBuilder sql = new StringBuilder("select tt.typeid,tt.typename,count(*) from t_type tt JOIN t_book tb on tt.typeid=tb.typeid GROUP BY tt.typeid ");
        sql.append(" ORDER BY tt.typeid LIMIT ?,?");
        List<Map<String, Object>> maps = JDBCUtil.queryListMap(sql.toString(),(type.getPage()-1)*type.getLimit(),type.getLimit());
        if(maps.size()>0){
            return maps;
        }
        return null;
    }

    @Override
    public int getTypeCount(Type type) {
        String sql = "select count(*) from t_type";
        List<List> lists = JDBCUtil.queryList(sql);
        if(lists.size()>0){
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 0;

    }

    @Override
    public int insertType(Type type) {
        String sql = "insert into t_type values(null,?)";
        int update = JDBCUtil.update(sql,type.getTypename());
        return update;
    }

    @Override
    public int deleteType(Integer typeid) {
        String sql = "delete from t_type where typeid=?";
        int update = JDBCUtil.update(sql, typeid);
        return update;
    }
}
